package com.xxx.yuedong.campaign.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.xxx.yuedong.campaign.controller.form.InsertPlanForm;
import com.xxx.yuedong.campaign.controller.form.InsertScheduleForm;
import com.xxx.yuedong.campaign.controller.form.SearchPlanByDateForm;
import com.xxx.yuedong.campaign.pojo.Plan;
import com.xxx.yuedong.campaign.pojo.Schedule;
import com.xxx.yuedong.campaign.service.ScheduleService;
import com.xxx.yuedong.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author hc
 * @date 2023/08/29 14:01
 **/

@RestController
@RequestMapping("/schedule")
@Api(tags = "日程相关接口")
@Slf4j
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("/insert")
    @ApiOperation(value = "日程中添加计划")
    public R insert(@Valid @RequestBody InsertScheduleForm form){
        log.debug("正在执行 【日程中添加计划】,参数为:{}" + form.toString());
        Schedule schedule = JSONUtil.parse(form).toBean(Schedule.class);
        schedule.setPlanId(JSONUtil.parseArray(form.getPlanId()).toString());
        schedule.setUserId(StpUtil.getLoginIdAsLong());
        int rows = scheduleService.insert(schedule);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/searchPlanByDate")
    @ApiOperation(value = "根据日期查询计划")
    public R insert(@Valid @RequestBody SearchPlanByDateForm form){
        log.debug("正在执行 【根据日期查询计划】,参数为:{}" + form.toString());
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("userId",StpUtil.getLoginIdAsLong());
        ArrayList<HashMap> list = scheduleService.searchPlanByDate(param);
        return R.ok().put("list",list);
    }
}
